/**
Copyright (c) 2013 cnova

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

library HashArray

    globals
        hashtable SparseArrayMasterTable = InitHashtable()
        integer SparseArrayStructCountPROTECTED = 0
    endglobals

    //! textmacro define_array takes N
    globals
        private integer array $N$
    endglobals
    //! endtextmacro
    
    //! textmacro define_sparse_handle_array takes N
    private struct $N$ extends array
        private static integer N
        static method operator [] takes handle h returns integer
            return LoadInteger(SparseArrayMasterTable, thistype.N, GetHandleId(h))
        endmethod
        static method operator []= takes handle h, integer s returns nothing
            call SaveInteger(SparseArrayMasterTable, thistype.N, GetHandleId(h), s)
        endmethod
        static method flush takes handle h returns nothing
            call RemoveSavedInteger(SparseArrayMasterTable, thistype.N, GetHandleId(h))
        endmethod
        static method hasKey takes handle h returns boolean
            return HaveSavedInteger(SparseArrayMasterTable, thistype.N, GetHandleId(h))
        endmethod
        static method remove takes handle h returns nothing
            call RemoveSavedInteger(SparseArrayMasterTable, thistype.N, GetHandleId(h))
        endmethod
        static method onInit takes nothing returns nothing
            set SparseArrayStructCountPROTECTED = SparseArrayStructCountPROTECTED + 1
            set thistype.N = SparseArrayStructCountPROTECTED
        endmethod
    endstruct
    //! endtextmacro
    
    //! textmacro define_sparse_array takes N
    private struct $N$ extends array
        private static integer N
        static method operator [] takes integer i returns integer
            return LoadInteger(SparseArrayMasterTable, thistype.N, i)
        endmethod
        static method operator []= takes integer i, integer s returns nothing
            call SaveInteger(SparseArrayMasterTable, thistype.N, i, s)
        endmethod
        static method flush takes integer i returns nothing
            call RemoveSavedInteger(SparseArrayMasterTable, thistype.N, i)
        endmethod
        static method hasKey takes integer i returns boolean
            return HaveSavedInteger(SparseArrayMasterTable, thistype.N, i)
        endmethod
        static method remove takes integer i returns nothing
            call RemoveSavedInteger(SparseArrayMasterTable, thistype.N, i)
        endmethod
        private static method onInit takes nothing returns nothing
            set SparseArrayStructCountPROTECTED = SparseArrayStructCountPROTECTED + 1
            set thistype.N = SparseArrayStructCountPROTECTED
        endmethod
    endstruct
    //! endtextmacro
    //! textmacro define_sparse_string_string_array takes N
    private struct $N$ extends array
        private static integer N
        static method operator [] takes string s returns string
            return LoadStr(SparseArrayMasterTable, thistype.N, StringHash(s))
        endmethod
        static method operator []= takes string s, string dat returns nothing
            call SaveStr(SparseArrayMasterTable, thistype.N, StringHash(s), dat )
        endmethod
        static method flush takes string s returns nothing
            call RemoveSavedString(SparseArrayMasterTable, thistype.N, StringHash(s))
        endmethod
        static method hasKey takes integer i returns boolean
            return HaveSavedString(SparseArrayMasterTable, thistype.N, StringHash(s))
        endmethod
        static method remove takes integer i returns nothing
            call RemoveSavedString(SparseArrayMasterTable, thistype.N,  StringHash(s))
        endmethod
        private static method onInit takes nothing returns nothing
            set SparseArrayStructCountPROTECTED = SparseArrayStructCountPROTECTED + 1
            set thistype.N = SparseArrayStructCountPROTECTED
        endmethod
    endstruct
    //! endtextmacro
    //! textmacro define_sparse_string_array takes N
    private struct $N$ extends array
        private static integer N
        static method operator [] takes integer i returns string
            return LoadStr(SparseArrayMasterTable, thistype.N, i)
        endmethod
        static method operator []= takes integer i, string dat returns nothing
            call SaveStr(SparseArrayMasterTable, thistype.N, i, dat )
        endmethod
        static method flush takes integer i returns nothing
            call RemoveSavedString(SparseArrayMasterTable, thistype.N, i)
        endmethod
        static method hasKey takes integer i returns boolean
            return HaveSavedString(SparseArrayMasterTable, thistype.N, i)
        endmethod
        static method remove takes integer i returns nothing
            call RemoveSavedString(SparseArrayMasterTable, thistype.N, i)
        endmethod
        private static method onInit takes nothing returns nothing
            set SparseArrayStructCountPROTECTED = SparseArrayStructCountPROTECTED + 1
            set thistype.N = SparseArrayStructCountPROTECTED
        endmethod
    endstruct
    //! endtextmacro
endlibrary